#include "SettingPage.h"
#include <QLabel>
#include <QPushButton>
#include <QComboBox>
#include <QLineEdit>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QCoreApplication>
#include <QSettings>
#include "../../models/CommonData.h"

QHash<QString,QLineEdit*> settingsLineList;
QHash<QString,QPushButton*> buttonList;

SettingPage::SettingPage(QWidget *parent)
    : QWidget{parent}
{
    setupUi();

    if(!buttonList.empty())
    {
        connect(buttonList["onSave"],&QPushButton::clicked,this,&SettingPage::saveConfig);
        connect(buttonList["onLoad"],&QPushButton::clicked,this,&SettingPage::loadConfig);
    }
    else
    {
        setupUi();
        connect(buttonList["onSave"],&QPushButton::clicked,this,&SettingPage::saveConfig);
        connect(buttonList["onLoad"],&QPushButton::clicked,this,&SettingPage::loadConfig);
    }

}

void SettingPage::loadConfig()
{
    QString path = QCoreApplication::applicationDirPath()+"/config.ini";
    QSettings settings(path,QSettings::IniFormat);

    BaseData<CommonData>::execute(&CommonData::setServerUrl,settings.value("url").toString().toStdString());
    BaseData<CommonData>::execute(&CommonData::setFactoryNo,settings.value("factoryNo").toString().toStdString());
    BaseData<CommonData>::execute(&CommonData::setMachineNo,settings.value("machineNo").toString().toStdString());
    BaseData<CommonData>::execute(&CommonData::setEventUser,settings.value("eventUser").toString().toStdString());

}

void SettingPage::saveConfig()
{
    QString path = QCoreApplication::applicationDirPath()+"/config.ini";
    QSettings settings(path,QSettings::IniFormat);

    if(!settingsLineList.empty())
    {
        std::string url = settingsLineList["serverUrl"]->text().toStdString();
        std::string factoryNo = settingsLineList["factoryNo"]->text().toStdString();
        std::string machineNo = settingsLineList["machineNo"]->text().toStdString();
        std::string user = settingsLineList["eventUser"]->text().toStdString();

        BaseData<CommonData>::execute(&CommonData::setServerUrl,url);
        BaseData<CommonData>::execute(&CommonData::setFactoryNo,factoryNo);
        BaseData<CommonData>::execute(&CommonData::setMachineNo,machineNo);
        BaseData<CommonData>::execute(&CommonData::setEventUser,user);
        settings.setValue("url",settingsLineList["serverUrl"]->text());
        settings.setValue("factoryNo",settingsLineList["factoryNo"]->text());
        settings.setValue("machineNo",settingsLineList["machineNo"]->text());
        settings.setValue("eventUser",settingsLineList["eventUser"]->text());
    }

}

void SettingPage::setupUi()
{
    loadConfig();
    initWidgets();
    initDatas();


}

void SettingPage::initWidgets()
{
    const auto btnLoadConfig = new QPushButton(tr("加载配置"));
    const auto btnSaveConfig = new QPushButton(tr("保存配置"));
    const auto paramGridLayout = new QGridLayout();
    const auto paramGroupBox = new QGroupBox(tr("参数设置"));
    const auto lbServerUrl = new QLabel(tr("服务器URL"));
    const auto txtServerUrl = new QLineEdit();
    const auto lbFactoryNo = new QLabel(tr("工厂编号"));
    const auto txtFactoryNo = new QLineEdit();
    const auto lbMachineNo = new QLabel(tr("设备编号"));
    const auto txtMachineNo = new QLineEdit();
    const auto lbEventUser = new QLabel(tr("操作员"));
    const auto txtEventUser = new QLineEdit();
    paramGridLayout->addWidget(lbServerUrl,0,0,1,1);
    paramGridLayout->addWidget(txtServerUrl,0,1,1,1);
    paramGridLayout->addWidget(lbFactoryNo,1,0,1,1);
    paramGridLayout->addWidget(txtFactoryNo,1,1,1,1);
    paramGridLayout->addWidget(lbMachineNo,2,0,1,1);
    paramGridLayout->addWidget(txtMachineNo,2,1,1,1);
    paramGridLayout->addWidget(lbEventUser,3,0,1,1);
    paramGridLayout->addWidget(txtEventUser,3,1,1,1);

    paramGroupBox->setLayout(paramGridLayout);

    const auto buttonLayout = new QHBoxLayout();
    buttonLayout->addWidget(btnLoadConfig);
    buttonLayout->addWidget(btnSaveConfig);

    const auto layout = new QGridLayout();
    layout->addLayout(buttonLayout,0,0,1,1);
    layout->addWidget(paramGroupBox,1,0,1,1);
    // layout->addItem(new QSpacerItem(120,0,QSizePolicy::Expanding,QSizePolicy::Minimum),0,1,1,1);

    setLayout(layout);

    settingsLineList.insert("serverUrl",txtServerUrl);
    settingsLineList.insert("factoryNo",txtFactoryNo);
    settingsLineList.insert("machineNo",txtMachineNo);
    settingsLineList.insert("eventUser",txtEventUser);

    buttonList.insert("onSave",btnSaveConfig);
    buttonList.insert("onLoad",btnLoadConfig);


}

void SettingPage::initDatas()
{
    if(!settingsLineList.empty())
    {
        loadConfig();
        settingsLineList["serverUrl"]->setText(QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getServerUrl)));
        settingsLineList["factoryNo"]->setText(QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getFactoryNo)));
        settingsLineList["machineNo"]->setText(QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getMachineNo)));
        settingsLineList["eventUser"]->setText(QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getEventUser)));
    }
    else
    {
        initWidgets();
        loadConfig();
        settingsLineList["serverUrl"]->setText(QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getServerUrl)));
        settingsLineList["factoryNo"]->setText(QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getFactoryNo)));
        settingsLineList["machineNo"]->setText(QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getMachineNo)));
        settingsLineList["eventUser"]->setText(QString::fromStdString(BaseData<CommonData>::execute(&CommonData::getEventUser)));
    }

}
